#!/bin/sh -eu
#
# Copyright (c) 2014-2021 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-or-later

./m4/gen_bpf_attr_m4.sh
./src/generate_mpers_am.sh
./src/xlat/gen.sh
./tests/gen_pure_executables.sh
./tests/gen_secontext.sh
./tests/gen_tests.sh

for m in m32 mx32; do
	m_upper=$(echo $m|tr '[a-z]' '[A-Z]')
	tests=tests-$m
	rm -rf $tests
	mkdir $tests
	s='[[:space:]]*'
	[ "$m" = m32 ] && k="s/^\\(SIZEOF_KERNEL_LONG_T$s=\\).*/\\1 4/;" || k=
	sed "s/@arch@/@arch_$m@/;
	     s/@karch@/@karch_$m@/;
	     s/^\\(SIZEOF_LONG$s=\\).*/\\1 4/; $k
	     s/^MPERS_NAME$s=.*/& $m/;
	     s/^\\(CC$s=\\).*/\\1 @CC_FOR_${m_upper}@/;
	     s/^MPERS_CC_FLAGS$s=.*/& @CFLAGS_FOR_${m_upper}@ @cc_flags_$m@/;
	     s/^ARCH_MFLAGS$s=.*/& -DMPERS_IS_\$(MPERS_NAME) \$(MPERS_CC_FLAGS)/; \
	     s/HAVE_SELINUX_RUNTIME/HAVE_${m_upper}_SELINUX_RUNTIME/" \
		tests/Makefile.am > $tests/Makefile.am
	for f in tests/*; do
		fname="${f##*/}"
		case "$fname" in
		Makefile*) continue;;
		*--secontext.c)
			sed "s/HAVE_SELINUX_RUNTIME/HAVE_${m_upper}_SELINUX_RUNTIME/" \
			    "$f" > $tests/"$fname"
			continue;;
		esac
		ln -s ../"$f" $tests/
	done
done

for f in README INSTALL; do
	cp "dist/$f" .
done

autoreconf -f -i "$@"
